const app = getApp()
Page({
  canIUse: wx.canIUse('button.open-type.getUserInfo'),
  avatarUrl:'',//头像
  schoolname:'',
  ufaceword:'',
  kefutel:'',
  loadingHidden: false,
  accounttype:'未绑定,普通用户',
  userInfo: {},
  bindInfo:null,
  isbind:false,
  hasUserInfo:false,
  handleSet: function (event) {
    wx.navigateTo({
      url: '../center/set'
    })
  },
  publish: function (event) {
    wx.navigateTo({
      url: '../publish/publish'
    })
  },
  //扫码
  sweepcode: function (event) {
    wx.scanCode({
      success: (res) => {
        var code = res.result;
        if (code == "" || code == null || code == undefined) {
          return;
        }
        wx.navigateTo({
          url: "/pages/sweepcode/sweepcode?code=" + code
        })
      },
      fail: (res) => {
      }
    })
  },
  //积分
  integral: function (event) {
    wx.navigateTo({
      url: '../integral/integral'
    })
  },
  //正向评价
  integralrecord: function (event) {
    wx.navigateTo({
      url: '../integralrecord/integralrecord'
    })
  },

  //调取相机限制一张图片 除了家长需要上传学生id
  handlePhoto: function (options) {
    if (!app.globalData.bindInfo){
      wx.redirectTo({
        url: '../binding/binding'
      })
      return ;
    }
    var uid = app.globalData.bindInfo.user_id;
    var openid = app.globalData.openid;
    var link = app.globalData.schlink;
   
    if (app.globalData.bindInfo.user_type == 3) {
      uid = app.globalData.bindInfo.stu_id;
    }
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({ loadingHidden: false });
        var tempFilePaths = res.tempFilePaths;
        if (res.tempFiles[0].size > 5242880){
          that.setData({ loadingHidden: true });
          wx.showToast({
            title: '图片不能超过5MB大小',
            icon: 'none',
            duration: 1500,
            mask: true
          })
          return ;
        }
        var serviceimage = [];
        if (tempFilePaths.length > 0) {
            wx.uploadFile({
              url: app.globalData.baseUrl + 'uploadUseravatar',
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                uid: uid,
                openid: openid,
                link: link
              },
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function (res) {
                that.setData({ loadingHidden: true });
                var resdata = JSON.parse(res.data);
                that.setData({ loadingHidden: true });
                if (resdata){
                  if (resdata.code == 404) {
                    wx.showToast({
                      title: resdata.msg,
                      icon: 'none',
                      duration: 1500,
                      mask: true
                    })
                  } else {
                    app.globalData.bindInfo.avatarUrl = resdata.data;
                    that.setData({
                      avatarUrl: resdata.data
                    })
                  }
                }
              }
            })
        }
      }
    })
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "我的"
    })
    var type="";
    var avatarUrl="";
    if (app.globalData.bindInfo){
      if (app.globalData.bindInfo.user_type==0){
        type="管理组";
      }
      if (app.globalData.bindInfo.user_type == 1) {
        type = "教师组";
      }
      if (app.globalData.bindInfo.user_type == 2) {
        type = "学生组";
      }
      if (app.globalData.bindInfo.user_type == 3) {
        type = "家长组";
      }
      if (app.globalData.bindInfo.user_type == 8) {
        type = "职员组";
      }
   
      if (app.globalData.bindInfo.user_avatar){
         avatarUrl = app.globalData.bindInfo.user_avatar;
      }else{
        if (app.globalData.userInfo){
          avatarUrl = app.globalData.userInfo.avatarUrl;
        }
      }
      that.setData({
        accounttype:type,
        bindInfo: app.globalData.bindInfo,
        isbind:true,
        loadingHidden:true,
        avatarUrl: avatarUrl
      })
    }else{
      that.revalidation();
    }
    that.getSchooldata(); //获取学校数据
    // that.getimgupdatephone(); //获取客服数据
    
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  //授权获取用户信息 授权之后存入缓存
  getUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo;
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        canIUse: false
      })
    }
  },
  setime: function (options) {
    var that = this;
    var timer; 
    timer = setTimeout(function () {
      that.getinfo();
    }, 6000);
  },

  //重新验证登录 获取用户信息
  getinfo: function () {
    var openid = app.globalData.openid;
    var type = "";
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'revalidation',
      method: "POST",
      data: {
        oauth: 'mapp',
        openid: openid,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (result) {
        if (result.data.code == 1) {
          if (result.data.data){
            that.setData({
              avatarUrl: result.data.data.user_avatar,
              loadingHidden: true
            })
          }
          app.globalData.bindInfo = result.data.data;
        }
        if (result.data.code == 0) {
          wx.redirectTo({
            url: '../binding/binding'
          });
          return;
        }
      }
    })
  },
  //重新验证登录 获取用户信息
  revalidation: function () {
    var openid = app.globalData.openid;
    var type = "";
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'revalidation',
      method: "POST",
      data: {
        oauth: 'mapp',
        openid: openid,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (result) {
        if (result.data.code==1){
          app.globalData.bindInfo = result.data.data;
          if (result.data.data.user_type == 0) {
            type = "管理组";
          }
          if (result.data.data.user_type == 1) {
            type = "教师组";
          }
          if (result.data.data.user_type == 2) {
            type = "学生组";
          }
          if (result.data.data.user_type == 3) {
            type = "家长组";
          }
          if (result.data.data.user_type == 8) {
            type = "职员组";
          }
          that.setData({
            accounttype: type,
            bindInfo: result.data.data,
            isbind: true,
            loadingHidden: true
          })
        }
        if (result.data.code == 0) {
          wx.redirectTo({
            url: '../binding/binding'
          });
          return;
        }
      }
    })
  },
  onReady: function () {
  },
  onShow: function () {
    var that=this;
    var type = "";
    var avatarUrl="";
    that.setime();
    if (app.globalData.bindInfo){
      if (app.globalData.bindInfo.user_type == 0) {
        type = "管理组";
      }
      if (app.globalData.bindInfo.user_type == 1) {
        type = "教师组";
      }
      if (app.globalData.bindInfo.user_type == 2) {
        type = "学生组";
      }
      if (app.globalData.bindInfo.user_type == 3) {
        type = "家长组";
      }
      if (app.globalData.bindInfo.user_type == 8) {
        type = "职员组";
      }
      if (app.globalData.bindInfo.user_avatar) {
        avatarUrl = app.globalData.bindInfo.user_avatar;
      } else {
        if (app.globalData.userInfo) {
          avatarUrl = app.globalData.userInfo.avatarUrl;
        }
      }
      that.setData({
        accounttype: type,
        bindInfo: app.globalData.bindInfo,
        isbind: true,
        avatarUrl: avatarUrl
      })
    }else{
      wx.redirectTo({
        url: '../binding/binding'
      })
    }
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {

  },

  
  onReachBottom: function () {

  },


  onShareAppMessage: function () {
  },

  getPhoneNumber(e) {
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) { 

        }
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '同意授权',
        success: function (res) {

         }
      })
    } 
  },
  getSchooldata: function (options) {
    var that=this;
    var link = app.globalData.schlink;//域名
    var openid = app.globalData.openid;
    wx.request({
      url: getApp().globalData.baseUrl + 'getSchoolname',
      data: {
        link: link,
        openid: openid,
      },
      method: "get",
      header: {},
      success: function (res) {
        if(res.data.code==200){
          if (res.data.data){
            app.globalData.schlink = res.data.data.link;
            that.getimgupdatephone(res.data.data.link);
            that.setData({ schoolname: res.data.data.schoolname});
          }
        }
      }
    })
  },
  getimgupdatephone: function (options) {
    var that = this;
    var link = options;//域名
    wx.request({
      url: getApp().globalData.baseUrl + 'getimgupdatephone',
      data: {
        link: link,
      },
      method: "get",
      header: {},
      success: function (res) {
        that.setData({ ufaceword:res.data.data1});
        that.setData({ kefutel: res.data.data2 });
      }
    })
  },
})